/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.common;

import com.google.gwt.core.client.GWT;

/**
 * @author aeky
 */
public class GWTUtils {

    /** The Constant CONTEXT_URL_REGEXP. */
    private static final String CONTEXT_URL_REGEXP = "[http|https]+:\\/\\/[0-9a-zA-Z.]*[:]*[0-9]*";

    /**
     * Provide the real context URL as it seems GWT.getHostPageBaseURL() does
     * not return what I expect...
     * 
     * @return the web application root context URL.
     */
    public static String getContextUrl() {
        if (GWT.getHostPageBaseURL().equals(GWT.getModuleBaseURL())) {
            final String ret = GWT.getHostPageBaseURL();
            int indexLast = ret.lastIndexOf(GWT.getModuleName());
            return ret.substring(0, indexLast).replaceAll(CONTEXT_URL_REGEXP, "");
        } else {
            return GWT.getHostPageBaseURL();
        }
    }
}
